package com.riko.comic.module.sys.controller;

import com.riko.comic.module.comic.bean.vo.ComicVo;
import com.riko.comic.module.sys.bean.po.ComicRank;
import com.riko.comic.module.sys.service.IComicRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wudechuang
 * @since 2021-11-01
 */
@Controller
@RequestMapping("/rank")
public class ComicRankController {

    @Autowired
    private IComicRankService comicRankService;

    @RequestMapping("/save")
    @ResponseBody
    public void saveOrUpdateRank(HttpServletRequest request, Integer comicId, Double score){
        Integer userId = (Integer) request.getSession().getAttribute("loginUserId");
        LocalDateTime time = LocalDateTime.now();

        ComicRank rank = new ComicRank();
        rank.setComicId(comicId);
        rank.setUserId(userId);
        rank.setScore(score);
        rank.setCreateTime(time);

        ComicRank comicRank = comicRankService.queryRank(rank);
        if (comicRank == null){
            comicRankService.save(rank);
        } else {
            comicRankService.updateById(rank);
            rank.setId(comicRank.getId());
            comicRankService.updateById(rank);
        }
    }

    @RequestMapping("remove")
    @ResponseBody
    public void removeRank(HttpServletRequest request, Integer comicId){
        int userId = (Integer) request.getSession().getAttribute("loginUserId");
        ComicRank rank = new ComicRank();
        rank.setComicId(comicId);
        rank.setUserId(userId);

        ComicRank comicRank = comicRankService.queryRank(rank);
        comicRankService.removeById(comicRank);
    }

    @RequestMapping("/init")
    @ResponseBody
    public ComicRank initRank(HttpServletRequest request, Integer comicId) throws IOException {
        Integer userId = (Integer) request.getSession().getAttribute("loginUserId");

        ComicRank rank = new ComicRank();

        rank.setComicId(comicId);
        rank.setUserId(userId);
        ComicRank comicRank = comicRankService.queryRank(rank);


        if(comicRank == null) {
            return new ComicRank();
        } else {
            return comicRank;
        }

    }

    @RequestMapping("/avgScore")
    @ResponseBody
    public Double queryAvgScore(Integer comicId){

        Double avgScore = comicRankService.queryAvgScore(comicId);
        return avgScore;
    }

    @RequestMapping("/ranking")
    public String ranking(HttpServletRequest request){
        LocalDate localDate = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime minTime = localDateTime.withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
        LocalDateTime maxTime = localDateTime.plusMonths(1L).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS).plus(-1L, ChronoUnit.MILLIS);

        HashMap<String, String> map1 = new HashMap<>();
        HashMap<String, String> map2 = new HashMap<>();
        map1.put("minTime", minTime.toString());
        map1.put("maxTime", maxTime.toString());
        map2.put("minTime", localDate.atTime(LocalTime.MIN).toString());
        map2.put("maxTime", localDate.atTime(LocalTime.MAX).toString());

        List<ComicVo> list1 = comicRankService.ranking();
        List<ComicVo> list2 = comicRankService.dayRanking(map1);
        List<ComicVo> list3 = comicRankService.monRanking(map2);

        request.setAttribute("comicList1", list1);
        request.setAttribute("comicList2", list2);
        request.setAttribute("comicList3", list3);

        return "rank";
    }

    @RequestMapping("/dayRanking")
    public String dayRanking(HttpServletRequest request){
        LocalDate localDate = LocalDate.now();
        HashMap<String, String> map = new HashMap<>();
        map.put("minTime", localDate.atTime(LocalTime.MIN).toString());
        map.put("maxTime", localDate.atTime(LocalTime.MAX).toString());

        List<ComicVo> list = comicRankService.dayRanking(map);
        request.setAttribute("comicList", list);

        return "";
    }

    @RequestMapping("/monRanking")
    public String monRanking(HttpServletRequest request){
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime minTime = localDateTime.withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
        LocalDateTime maxTime = localDateTime.plusMonths(1L).withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS).plus(-1L, ChronoUnit.MILLIS);

        HashMap<String, String> map = new HashMap<>();
        map.put("minTime", minTime.toString());
        map.put("maxTime", maxTime.toString());

        List<ComicVo> list = comicRankService.ranking();
        request.setAttribute("comicList", list);

        return "";
    }
}
